<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css"
        integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <script src="http://lib.sinaapp.com/js/jquery/3.1.0/jquery-3.1.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
        integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous"></script>
    <style>
        html,
        body {
            height: 100%;
            width: 100%;
            font-size: 14px;
        }

        * {
            margin: 0;
            padding: 0;
        }

        .form {
            position: relative;
            top: 140px;
            width: 400px;
            height: 300px;
            margin: 0 auto;
            border: 1px saddlebrown;
        }
    </style>
</head>

<body>
    <div class="form">
        <div class="form-group">
            <label for="exampleInputEmail1">用户名</label>
            <input type="text" class="form-control" id="acc" placeholder="">
            <small class="form-text text-muted">考生的准考證號碼</small>
        </div>
        <div class="form-group">
            <label for="exampleInputPassword1">密碼</label>
            <input type="password" class="form-control" id="pwd" placeholder="">
            <small class="form-text text-muted">考生的默認密碼為身份證號后六位</small>
        </div>
        <button type="button" id="btn" class="btn btn-default">登入</button>
    </div>
    <script>
        $(function () {
            $('#btn').click(function () {
                if ($('#acc').val().search('admin')==-1) {
                    var testnumber = $('#acc').val();
                    var password = $('#pwd').val();
                    if (testnumber.length == 0 || password.length == 0)
                        alert('账号或密码不能为空');
                    $.post('http://localhost:8083/php/index.php?c=login', {
                        testnumber,
                        password
                    }, function (rs) {
                        if (rs.code == 200) {
                            sessionStorage['id'] = rs.data[0].id;
                            sessionStorage['testnumber'] = rs.data[0].testnumber;
                            window.location = 'index.html'
                        } else if (rs.code == 100) {
                            alert('密码或账号错误');
                        } else {
                            alert('未知错误');
                        }
                    })
                }else{
                    var name = $('#acc').val();
                    var password = $('#pwd').val();
                    if (name.length == 0 || password.length == 0)
                        alert('账号或密码不能为空');
                    $.post('http://localhost:8083/php/admin.php?c=login', {
                        name,
                        password
                    }, function (rs) {
                        if (rs.code == 200) {
                            sessionStorage['id'] = rs.data.id;
                            sessionStorage['name'] = rs.data.name;
                            window.location = 'admin.html'
                        } else if (rs.code == 100) {
                            alert('密码或账号错误');
                        } else {
                            alert('未知错误');
                        }
                    })
                }
            })
        })
    </script>
</body>

</html>